<template>
  <div>
    <el-row :gutter="10" class="mb8">
      <el-col :span="18"><span style="font-weight:blod"></span>材料参数</el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-delete"
          size="mini"
          :disabled="multipleSet"
          @click="handleDeleteSelect"
          >删除</el-button
        >
      </el-col>
    </el-row>
    <el-table
      v-loading="loading"
      :data="setSelectItems"
      @selection-change="handleSelectionChangeSet"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="参数名" align="center" prop="paramName" />
      <el-table-column label="参数值" align="center" prop="valueNumber">
        <template slot-scope="scope">
          <el-input
            v-model="scope.row.valueNumber"
            placeholder="请输入参数值"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row.paramId)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        label-width="68px"
      >
        <el-form-item label="参数名称" prop="paramName">
          <el-input
            v-model="queryParams.paramName"
            placeholder="请输入名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
      <el-table
        v-loading="loading"
        :data="paramList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="参数ID" align="center" prop="paramId" />
        <el-table-column label="参数名称" align="center" prop="paramName" />
        <el-table-column label="单位" align="center" prop="unitName" />
        <el-table-column label="备注" align="center" prop="remark" />
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" v-funThrottle="[submitForm]">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listParam } from "@/api/base/dataType";
import { removeByValue } from '@/utils/index'
const configId = require('@/utils/configId')
export default {
  data() {
    return {
      title: "选择参数",
      multiple: true,
      multipleSet:true,
      paramList: [],
      loading: false,
      form: {},
      open: false,
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        paramName: null,
        paramCategoryId: configId.materialCategoryId,//材料参数分类Id
      },
      ids:[],
      selectItems: [], //选中的参数
      setSelectItems: [],
    };
  },
  methods: {
    setTable(data){
      this.setSelectItems=data.map(item=>{
        return { paramId: item.paramId, paramName: item.paramName, valueNumber: item.valueNumber };
      })
    },
    submitForm() {
      if (this.setSelectItems.length == 0) {
        this.setSelectItems = JSON.parse(JSON.stringify(this.selectItems));
        this.cancel();
        return;
      }
      let arr = this.selectItems.filter(
        (item) => !this.setSelectItems.some((ele) => ele.paramId === item.paramId)
      );
      arr.forEach((item) => {
        this.setSelectItems.push(item);
      });
      this.cancel();
    },
    resetQuery() {
      this.queryParams.paramName = "";
      this.handleQuery();
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    handleAdd() {
      this.open = true;
      this.getList();
    },
    handleDelete(id) {
        removeByValue(this.setSelectItems,'paramId',id)
    },
    handleDeleteSelect(){
       this.ids.forEach(item=>{
           removeByValue(this.setSelectItems,'paramId',item)
       })
    },
    cancel() {
      this.open = false;
      this.selectItems = [];
    },
    handleSelectionChangeSet(selection){
         this.ids = selection.map((item) => item.paramId);
     
      this.multipleSet = !selection.length;
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.selectItems = selection.map((item) => {
        return {paramId: item.paramId, paramName: item.paramName, valueNumber: "" };
      });
      this.multiple = !selection.length;
    },

    /** 查询参数定义列表 */
    getList() {
      this.loading = true;
      listParam(this.queryParams).then((response) => {
        this.paramList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
  },
};
</script>

<style>
</style>